package test;

import jtarget.data.model.ShootingLane;
import jtarget.server.ServiceData;
import jtarget.view.board.ScreenBoard;
import jtarget.view.board.SmallTargetBoardView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class testDrag extends JDialog  implements MouseMotionListener, MouseListener{
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JPanel panel1;
    SmallTargetBoardView view = new SmallTargetBoardView();
    SmallTargetBoardView view2 = new SmallTargetBoardView();
    static Color selectedColor = new Color(180, 200, 250);
    static Color unselectedColor = new Color(255,255,255,0);


    public testDrag() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    public void addView(){
        view.screenSize = ScreenBoard.SIZE_Preview;
        ShootingLane lane = new ShootingLane();
        lane.setBoardId(1);
        lane.setLaneId(1);
        lane.setBoardType(1);
        lane.addListener(view);
        view.setScreenBoardModel(lane);
        view.initialize();
        view.setBounds(0, 0, (int)view.dim.getWidth(), (int)view.dim.getHeight());
        panel1.setLayout(null);
        panel1.add(view);

        view2.screenSize = ScreenBoard.SIZE_Preview;
        lane = new ShootingLane();
        lane.setBoardId(2);
        lane.setLaneId(2);
        lane.setBoardType(0);
        lane.addListener(view);
        view2.setScreenBoardModel(lane);
        view2.initialize();
        view2.setBounds(200, 200, (int)view.dim.getWidth(), (int)view.dim.getHeight());
        panel1.add(view2);

        panel1.addMouseMotionListener(this);
        panel1.addMouseListener(this);
    }

    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        ServiceData data = ServiceData.getInstance();
        data.initialize();
        testDrag dialog = new testDrag();
        dialog.addView();
        dialog.setSize(new Dimension(600, 600));
        //dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    int x1, y1, x2, y2;
    SmallTargetBoardView selectedView = null;

    @Override
    public void mousePressed(MouseEvent e) {
        x1 = e.getX();
        y1 = e.getY();
        if(view.getBounds().contains(x1, y1)) selectedView = view;
        if(view2.getBounds().contains(x1, y1)) selectedView = view2;
        if(selectedView != null) selectedView.setBackground(selectedColor);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
        if(selectedView != null){
            selectedView.setBounds(x2, y2, (int)selectedView.dim.getWidth(), (int)selectedView.dim.getHeight());
            panel1.repaint();
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        view.setBackground(unselectedColor);
        panel1.repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }



    @Override
    public void mouseMoved(MouseEvent e) {

    }
}
